<template>
  <div style="padding-bottom: 7vh" id="mainTop">
    <div class="scalable-page">
      <headers></headers>
      <router-view></router-view>
      <bottoms :config="webConfig"></bottoms>
    </div>
    <div class="xianxia">
      <div class="centerxia">
        <div class="link">
          <a href="https://qs.kdxwindowfilm.com/qualitySearch" target="_blank">
            <img class="biaoshi" src="/static/index1.png" />
            <div>质保查询</div>
          </a>
        </div>
        <div @click="goStore" class="link">
          <img style="width: 80px" class="biaoshi" src="/static/index2.png" />
          <div>线下渠道</div>
        </div>
        <div
          class="qu link"
          @click="showqudao = !showqudao"
          style="position: relative"
        >
          <img class="biaoshi" src="/static/index3.png" />
          <div>线上渠道</div>
          <div class="online-tooltip">
            <a href="https://kdx.tmall.com/" target="_blank">
              <img src="/static/tmall.png" alt="天猫" />
            </a>
            <a href="https://mall.jd.com/index-12761077.html" target="_blank">
              <img src="/static/jd.png" alt="京东" />
            </a>
            <a href="javascript:void(0);" @click="showDouyinPopup">
              <img src="/static/dy.png" alt="抖音" />
            </a>
          </div>
        </div>
      </div>
    </div>
    <!-- 抖音弹窗 -->
    <div v-if="showDouyinModal" class="modal-overlay" @click="closeDouyinModal">
      <div class="modal-content" @click.stop>
        <img
          :src="'/static/dyshare.jpg'"
          alt="抖音分享"
          style="max-width: 500px; max-height: 80vh"
        />
        <button @click="closeDouyinModal" class="close-btn">关闭</button>
      </div>
    </div>
  </div>
</template>
<script>
import headers from "/components/headers.vue";
import bottoms from "/components/bottoms.vue";
import { get } from "@/utils/request";
import { useConfigStore } from "@/store/config";

export default {
  name: "App",
  components: { headers, bottoms },
  data() {
    return {
      // 保留data中的webConfig以备兼容使用
      webConfig: {},
      showDouyinModal: false,
      showqudao: false,
    };
  },
  provide() {
    return {
      showDouyinModal: this.showDouyinPopup, // 提供方法给子组件
      closeDouyinModal: this.closeDouyinModal, // 如果需要也可以提供关闭方法
    };
  },
  created() {
    const isMobile =
      /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
        navigator.userAgent
      );
    console.log(isMobile);
    if (isMobile) {
      // 如果是手机 那么跳转到手机端
      // window.location.href = "http://www.kdxwindowfilm.com/H5/index.html"
    } else {
      console.log("当前是PC端访问");
    }
  },
  methods: {
    async getWebconfig() {
      try {
        const res = await get("/setting/getAll");
        if (res.code === 200) {
          // 更新本地data
          this.webConfig = res.data;
          // 更新localStorage
          localStorage.setItem("webConfig", JSON.stringify(res.data));
          // 更新Pinia store
          const configStore = useConfigStore();
          configStore.setWebConfig(res.data);

          // 动态设置网页标题
          // document.title = res.data.site_name || "默认标题";
        }
      } catch (error) {
        console.error("获取网站配置失败:", error);
        // document.title = "上海康得新晨诺光学材料有限公司";
      }
    },

    showDouyinPopup() {
      // this.$router.push({
      //   path: "/dyshare",
      // });
      // 模拟router.resolve方法
      // const fullUrl = window.location.origin + '/dyshare';
      // window.open(fullUrl, '_blank');
      this.showDouyinModal = true;
    },
    closeDouyinModal() {
      this.showDouyinModal = false;
    },
    goStore() {
      this.$router.push("/store");
    },
  },
  mounted() {
    this.getWebconfig();

    // 动态计算缩放比例
    const updateScale = () => {
      const scale = window.innerWidth / 1920;
      document.documentElement.style.setProperty("--scale", scale);
    };

    window.addEventListener("resize", updateScale);
    updateScale(); // 初始化计算
  },
  watch: {
    $route() {
      const configStore = useConfigStore();
      if (configStore.webConfig.site_name) {
        // document.title = configStore.webConfig.site_name;
      }
      const scalablePage = document.querySelector("#app");
      if (scalablePage) {
        console.log(scalablePage.scrollTop);
        scalablePage.scrollTop = 0;
      }
    },
  },
};
</script>
<style lang="less">
.online-tooltip {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  display: none;
  z-index: 100;
  width: 300px;
  text-align: center;

  &::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border-width: 8px;
    border-style: solid;
    border-color: white transparent transparent transparent;
  }

  a {
    display: inline-block;
    margin: 0 10px;

    img {
      width: 66px;
      height: 66px;
      transition: transform 0.3s;

      &:hover {
        transform: scale(1.2);
      }
    }
  }
}

.xianxia .centerxia .link {
  padding: 15px 0;
  width: 350px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  justify-content: center;
  & > a {
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 19px;
    color: #000;
    text-decoration: none;
    gap: 20px;
    &:hover {
      color: #a30008;
    }
  }
  &:hover {
    color: #a30008;
  }
}

.centerxia {
  display: flex;
  justify-content: center;
  justify-items: center;
  width: 1366px;
  margin: 0 auto;
}

.xianxia {
  transform: scale(var(--scale));
  transform-origin: 0 100%;
  width: 1920px;
  margin: 0 auto;
  // height: 9vh;
  box-sizing: border-box;

  position: fixed;
  bottom: -1px;
  background: white;
}

.biaoshi {
  width: 30px;
  display: block;
  object-fit: contain;
  /* 保持比例，填充容器 */
}

.xianxia .centerxia > div > div:nth-child(2) {
  font-size:30px;
  line-height: 55px;
  height: 55px;
  margin-left: 22px;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

.xianxia > .centerxia {
  flex: 1;
  display: flex;
  justify-content: center;
  justify-items: center;
  position: relative;
  // top: 50%;
  // transform: translateY(-50%);
  // position: relative;
}
.xianxia .centerxia .link a{
  font-size: 30px;
}
.qu:hover .online-tooltip {
  display: block;
  animation: fadeIn 0.3s ease;
}

#app {
  overflow: hidden;
  width: 100vw;
  height: 100vh;
  overflow: auto;

  .highcharts-credits {
    display: none;
  }
}

:root {
  --scale: 1;
}

html,
body {
  margin: 0;
  padding: 0;
  overflow: hidden;
  width: 100vw;
  height: 100vh;
}

.scalable-page {
  /* transform: scale(var(--scale)); */
  transform-origin: 0 0;
  width: 1920px;
  /* 设计稿宽度 */
  height: 1080px;
  // overflow: auto;
  /* 设计稿高度 */
  box-sizing: border-box;
  position: relative;

  transform: scale(var(--scale));
  /* transform: translateX(-50%); */
}

.box {
  width: 300px;
  height: 200px;
  background: #e74c3c;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
}

/* 抖音弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.modal-content {
  background-color: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  transform: scale(var(--scale));
}

.close-btn {
  margin-top: 20px;
  padding: 10px 25px;
  background-color: #a30008;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    background-color: #c5000a;
    transform: translateY(-2px);
  }
}

/* 弹窗动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-content {
  animation: fadeIn 0.3s ease-out;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .modal-content {
    width: 90%;
    padding: 20px;

    img {
      max-width: 100%;
      height: auto;
    }
  }

  .close-btn {
    padding: 8px 20px;
    font-size: 14px;
  }
}
</style>